package com.gsta.cdn.cdr.controller;

import com.gsta.cdn.cdr.entity.ResultEntity;
import com.gsta.cdn.cdr.entity.ServerTypeIpTimes;
import com.gsta.cdn.cdr.mapper.ServerTypeIpTimesMapper;
import com.gsta.cdn.cdr.model.ResultModel;
import com.gsta.cdn.cdr.service.ServerTypeIpTimesService;
import com.gsta.cdn.cdr.utils.ResultTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(path = "/cdr")
@RestController
public class ServerTypeIpTimesController {

    @Autowired
    private ServerTypeIpTimesMapper serverTypeIpTimesMapper;

    @Resource
    private ServerTypeIpTimesService serverTypeIpTimesService;

    @RequestMapping(path = "/selectServerTypeIpTimesByAll")
    public ResultModel selectUserByAll() {
        try {
            List<ServerTypeIpTimes> userLs = serverTypeIpTimesMapper.selectServerTypeIpTimesByAll();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("content", userLs);
            return ResultTools.result(0, "", map);
        } catch (Exception e) {
            return ResultTools.result(444, e.getMessage(), null);
        }
    }

    @RequestMapping(value = { "/selectUserById" }, method = RequestMethod.GET)
    public String test(){
        System.out.println("hello test");
        return "test";
    }

    @RequestMapping(path = "/selectSum/{date}")
    public ResultEntity selectSum(@PathVariable String date) {
        List<Map<String, Object>> list = serverTypeIpTimesService.selectSum(date);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            resultEntity.setCount(serverTypeIpTimesService.countSum(date));
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

}



